package com.binance.mbxgateway.web.controller;

import java.util.List;

import com.binance.mbxgateway.vo.request.api.ApiKeyModelRequest;
import com.binance.mbxgateway.vo.request.api.GetApiListRequest;
import com.binance.mbxgateway.vo.request.api.SearchDeletedApiRequest;
import com.binance.master.error.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.binance.master.models.APIRequest;
import com.binance.master.models.APIResponse;
import com.binance.mbxgateway.api.IApi;
import com.binance.mbxgateway.vo.request.api.DeleteAllApiKeyRequest;
import com.binance.mbxgateway.vo.request.api.DeleteApiKeyRequest;
import com.binance.mbxgateway.vo.request.api.EnableApiCreateRequest;
import com.binance.mbxgateway.vo.request.api.EnableApiWithdrawRequest;
import com.binance.mbxgateway.vo.request.api.GetApisRequest;
import com.binance.mbxgateway.vo.request.api.SaveApiKeyRequest;
import com.binance.mbxgateway.vo.request.api.SearchApiRequest;
import com.binance.mbxgateway.vo.request.api.UpdateApiKeyRequest;
import com.binance.mbxgateway.vo.response.PagingResult;
import com.binance.mbxgateway.vo.response.api.ApiModelResponse;
import com.binance.mbxgateway.vo.response.api.EnableApiWithdrawResponse;
import com.binance.mbxgateway.vo.response.api.UpdateApiKeyResponse;
import com.binance.mbxgateway.web.service.IApiService;

@RestController
@Slf4j
public class ApiController implements IApi {

    @Autowired
    private IApiService apiService;

    @Override
    public APIResponse<PagingResult<ApiModelResponse>> loadApiList(
            @RequestBody @Validated APIRequest<SearchApiRequest> request) throws Exception {
        try {
            return APIResponse.getOKJsonResult(this.apiService.loadApiList(request.getBody()));
        } catch (Exception e) {
            log.warn("ApiController.loadApiList occurs error", e);
            throw e;
        }
    }

    @Override
    public APIResponse<PagingResult<ApiModelResponse>> loadDeletedApiList(
            @RequestBody @Validated APIRequest<SearchDeletedApiRequest> request) throws Exception {
        try{
            return APIResponse.getOKJsonResult(this.apiService.loadDeletedApiList(request.getBody()));
        }catch(Exception e){
            log.warn("ApiController.loadDeletedApiList occurs error", e);
            throw e;
        }
    }

    @Override
    public APIResponse<Void> addDeletedApi(@RequestBody @Validated APIRequest<ApiKeyModelRequest> request)
            throws Exception {
        this.apiService.addDeletedApi(request.getBody());
        return APIResponse.getOKJsonResult();
    }

    @Override
    public APIResponse<ApiModelResponse> queryApiByUserAndApiKey(
            @RequestBody @Validated APIRequest<SearchApiRequest> request) throws Exception {
        try {
            return APIResponse.getOKJsonResult(this.apiService.queryApiByUserAndApiKey(request.getBody().getUserId(),
                    request.getBody().getApiKey()));
        } catch (Exception e) {
            log.warn("ApiController.queryApiByUserAndApiKey occurs error", e);
            throw e;
        }
    }

    @Override
    public APIResponse<ApiModelResponse> createApiKey(@RequestBody @Validated APIRequest<SaveApiKeyRequest> request)
            throws Exception {
        try {
            return APIResponse.getOKJsonResult(this.apiService.saveApiKey(request.getBody()));
        } catch (Exception e) {
            log.warn("ApiController.createApiKey occurs error", e);
            throw e;
        }
    }

    @Override
    public APIResponse<ApiModelResponse> enableApiCreate(
            @RequestBody @Validated APIRequest<EnableApiCreateRequest> request) throws Exception {
        try {
            return APIResponse.getOKJsonResult(this.apiService.enableApiCreate(request.getBody()));
        } catch (Exception e) {
            log.warn("ApiController.enableApiCreate occurs error", e);
            throw e;
        }
    }

    @Override
    public APIResponse<UpdateApiKeyResponse> updateApiKey(
            @RequestBody @Validated APIRequest<UpdateApiKeyRequest> request) throws Exception {
        try {
            return APIResponse.getOKJsonResult(this.apiService.updateApiKey(request.getBody()));
        } catch (Exception e) {
            log.warn("ApiController.updateApiKey occurs error", e);
            throw e;
        }
    }

    @Override
    public APIResponse<Void> deleteApiKey(@RequestBody @Validated APIRequest<DeleteApiKeyRequest> request)
            throws Exception {
        try {
            this.apiService.deleteApiKey(request.getBody());
            return APIResponse.getOKJsonResult();
        } catch (Exception e) {
            log.warn("ApiController.deleteApiKey occurs error", e);
            throw e;
        }
    }

    // @Override
    // public APIResponse<Void> reSendMailVerify(@RequestBody @Validated
    // APIRequest<ReSendMailVerifyRequest> request)
    // throws Exception {
    // this.apiService.reSendMailVerify(request.getBody());
    // return APIResponse.getOKJsonResult();
    // }

    @Override
    public APIResponse<EnableApiWithdrawResponse> enableApiWithdraw(
            @RequestBody @Validated APIRequest<EnableApiWithdrawRequest> request) throws Exception {
        try {
            return APIResponse.getOKJsonResult(this.apiService.enableApiWithdraw(request.getBody()));
        } catch (Exception e) {
            log.warn("ApiController.enableApiWithdraw occurs error", e);
            throw e;
        }
    }

    // @Override
    // public APIResponse<Void> reSendMail(@RequestBody @Validated APIRequest<ReSendMailRequest>
    // request)
    // throws Exception {
    // this.apiService.reSendMail(request.getBody());
    // return APIResponse.getOKJsonResult();
    // }

    @Override
    public APIResponse<Void> deleteAllApiKey(@RequestBody @Validated APIRequest<DeleteAllApiKeyRequest> request)
            throws Exception {
        try {
            this.apiService.deleteAllApiKey(request.getBody());
            return APIResponse.getOKJsonResult();
        } catch (Exception e) {
            log.warn("ApiController.deleteAllApiKey occurs error", e);
            throw e;
        }
    }

    @Override
    public APIResponse<List<ApiModelResponse>> getApis(@RequestBody @Validated APIRequest<GetApisRequest> request)
            throws Exception {
        try {
            return APIResponse.getOKJsonResult(this.apiService.getApis(request.getBody()));
        } catch (Exception e) {
            log.warn("ApiController.getApis occurs error", e);
            throw e;
        }
    }

    @Override
    public APIResponse<List<ApiModelResponse>> getApiList(@RequestBody @Validated APIRequest<GetApiListRequest> request) throws Exception {
        try {
            return APIResponse.getOKJsonResult(this.apiService.getApiList(request.getBody()));
        } catch (Exception e) {
            log.warn("ApiController.getApiList occurs error", e);
            throw e;
        }
    }

    // @Override
    // public APIResponse<Void> disableApiById(APIRequest<DisableApiByIdRequest> request) throws
    // Exception {
    // this.apiService.disableApiById(request.getBody());
    // return APIResponse.getOKJsonResult();
    // }


}
